Skip to content

Use the screened short-needle search for find, contains, and rfind - #188

Merged
mxsm merged 4 commits into
mainfrom
mxsm/screened-search
Nov 20, 2026
Merged

mxsm merged 4 commits into
mainfrom
mxsm/screened-search

Conversation

@mxsm

@mxsm mxsm commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes the last review finding in #186: the setup-free screened search only served split_str, while find, contains, and rfind paid searcher setup for the same short needles.

Summary

  • find_short, ShortSearch, and their constants move from pattern.rs to search.rs, and split_str imports them. rfind_short is the same screen scanning blocks from the end, taking the highest candidate first
  • new dispatch for 2- to 32-byte needles:
    • find and rfind screen haystacks under 256 bytes
    • contains screens only where std's SIMD contains cannot run: before the haystack holds a 16-byte block plus the needle, or under 256 bytes on targets without that fast path
    • longer haystacks keep one-shot memmem for find and str::rfind for rfind
    • the str::find arm (16 + 3n <= L < 64) can no longer be reached, so it and its constants are removed
  • after four false candidates, each search hands the text it has not ruled out to a Two-Way searcher:
    • find and contains continue from the last false candidate, which holds the needle's first byte and so is a character boundary
    • rfind cuts the text at the first character boundary at or after last candidate + needle - 1. The up to three extra bytes cannot hold a match start, because those positions were already ruled out
  • each screened arm is one out-of-line call (contains_screened, find_screened, rfind_screened) that runs the screen and its hand-off and returns the final answer. With the screen inlined, or with the dispatch matching on its result, the other arms paid 2 to 6 ns or saved and restored four registers on every call. split_str still inlines find_short
  • docs: crate docs, contains/find/rfind docs, README search section, and the CHANGELOG search entry describe the new rules

Measured impact

benches/search_thresholds.rs on an i7-11700K, CheetahString times compared with main over every input of the sweep (geometric mean):

Operation All inputs Haystacks under 256 B Against the faster of str and memmem
find 0.59x 0.50x 0.64x (was 1.07x)
rfind 0.52x 0.43x 0.59x (was 1.09x)
contains 1.00x 0.99x 1.12x (was 1.10x)

Examples, ns per call:

Search main This PR
find, 8-byte needle, 32 B, miss 25.2 9.8
find, 16-byte needle, 64 B, miss 46.2 9.0
find, 32-byte needle, 64 B, miss 70.3 7.8
rfind, 16-byte needle, 64 B, miss 53.5 8.2
rfind, 4-byte needle, 128 B, miss 46.4 16.3
contains, 32-byte needle, 40 B, miss 20.3 8.4

Costs:

  • 16-byte haystacks with 2-byte needles: 1–2 ns slower for find and contains; they never fill a screening block and take the scalar tail
  • unchanged long-haystack paths: in the sweep they moved by up to ±16% between runs. In a same-binary A/B they were within 2% (rfind 512 B–4 KiB) and within 8% (find 512 B, which moved between 1.01x and 1.15x across runs). contains on the std path was 0.2–0.4 ns slower in the A/B
  • the instruction-count gate reports the exact cost of each unchanged path on this PR

Variants measured and dropped:

  • screening every haystack length: find on 4 KiB with a 2-byte needle went from 90 to 250 ns, and contains from std's SIMD path got up to 2.9x slower
  • screened searches inlined into the dispatch: the other arms became 2–6 ns slower
  • the long-haystack paths moved into cold helpers: contains on 128 B took 2x as long

CI failure and fix

The instruction-count gate failed contains/32B_miss, an input that takes std's SIMD arm and never reaches the screen. It took three steps to fix:

  1. 138 to 151 instructions. contains_str matched on the out-of-line screen's result and could call the hand-off. That kept its inputs live across the call, so it saved and restored four registers on every call. Fix: each screened arm became one call that returns the final answer.
  2. 145 instructions. The scenario's own code was now the same as on main; I checked by building the gate harness with --emit asm for both sides. The difference was inside std_contains. The screened hand-off called str::contains directly, a second call site, so LLVM stopped inlining core's search into std_contains. That made it a seven-instruction shuffle and jump to a shared copy.
  3. 138 instructions, same as main. The contains and rfind hand-offs now call the std_contains and std_rfind wrappers, so each std search keeps one call site. In the harness build, std_contains and std_rfind compile to the same 297 and 283 instructions as on main.

Final gate, instructions per call:

Scenario Base Head
contains/32B_miss 138 138
find/48B_miss 586 288 (-50.9%)
rfind/48B_miss 646 223 (-65.5%)
find/1KiB_miss 997 982 (-1.5%)

CI sweeps

Search thresholds on the final commit. CheetahString time against the faster of str and memmem in the same run, geometric mean over the sweep. This is compared within one run because hosted runners differ from run to run:

Operation ubuntu-latest before after ubuntu-24.04-arm before after
find 1.09 0.66 1.04 0.69
rfind 1.04 0.56 1.00 0.53
contains 1.20 1.12 1.16 1.12

On aarch64, a 2-byte needle in a 192-byte haystack is the weakest point: 26.8 to 34.3 ns, where NEON memmem beats the screen near the 256-byte limit.

Verification

  • cargo fmt --check; clippy -D warnings for all targets with all features, no default features, and serde,bytes,simd
  • cargo test --all-features: 248 passed, 2 of them new:
    • search at every offset of texts up to 70 bytes and at 100–300 bytes, around the 256-byte limit, for five needles including a multi-byte one
    • the hand-offs, with ASCII and multi-byte needles
  • --no-default-features --lib: 17 passed; doctests: 38 passed
  • rustdoc without warnings; cargo +1.95.0 check --all-features; Python tests pass
  • mutation checks:
    • taking the lowest instead of the highest reverse candidate fails three search tests
    • cutting the rfind hand-off text one byte short fails the hand-off test, through a case whose last match ends exactly at the cut
  • this PR's Search thresholds run measures the new dispatch on x86_64 and aarch64

Closes #186

🤖 Generated with Claude Code

The setup-free screened search that split_str uses moves to search.rs as
find_short, gains a reverse twin, rfind_short, and now serves find and
rfind for 2- to 32-byte needles in haystacks under 256 bytes, and contains
where std's SIMD contains cannot run. Longer haystacks keep memmem for find
and str::rfind for rfind; the str::find arm, unreachable under the new
rule, is removed. After four false candidates each search hands the text
it has not ruled out to a Two-Way searcher; the rfind hand-off cuts the
text at the first character boundary at or after the last possible match
end.

The screened searches are called out of line from the dispatch: inlined,
they cost the other arms 2 to 6 ns per call. On an i7-11700K find takes
0.59 and rfind 0.52 of their previous time across the threshold sweep,
contains is unchanged overall. Differential tests cover every offset
across block boundaries and the 256-byte limit, and the hand-offs with
ASCII and multi-byte needles, including a match that ends exactly at the
reverse cut.

Closes #186

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 07:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

mxsm and others added 3 commits November 19, 2026 23:17
The instruction-count gate failed contains/32B_miss, 138 -> 151
instructions, although that input takes std's SIMD arm. After the
out-of-line screen call, contains_str still matched on its result and
could call the hand-off, so the inputs stayed live across the call and
the function saved and restored four registers on every call, whatever
the arm.

contains_screened, find_screened, and rfind_screened now each run the
screen and its hand-off and return the final answer, so every dispatch
arm is a single call. contains_str's prologue matches main again; the
same-binary A/B keeps every unchanged path within 5% of main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The instruction-count gate still measured contains/32B_miss, which takes
std's SIMD arm, at 145 instructions against 138 on main. With the screen
as a separate out-of-line arm next to the inline memmem call, the
inlined contains_str compiled differently around the std arm.

contains_str now keeps main's arms up to and including the std arm, and
sends every other input to contains_without_std_simd, which picks between
the screen and memmem out of line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gate still measured contains/32B_miss at 145 instructions against
138. Its scenario code was identical; the difference was in
std_contains. The screened hand-off called str::contains directly, a
second call site, so LLVM stopped inlining core's search into
std_contains and turned it into a seven-instruction shuffle and jump to a
shared copy.

The contains and rfind hand-offs now call std_contains and std_rfind, so
each std search keeps one call site. In the harness build, std_contains
and std_rfind compile to the same 297 and 283 instructions as on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mxsm
mxsm merged commit 631f202 into main Nov 20, 2026
19 checks passed
@mxsm
mxsm deleted the mxsm/screened-search branch November 20, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Follow-ups from the review of #181, #183, and #185

2 participants